package com.tracking.application.activities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import com.tracking.application.util.CommonModuleApp;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Service GPS location
 * Date: 24.11.13
 * Time: 02:45
 */
public class GpsTrackerBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String locationKey = LocationManager.KEY_LOCATION_CHANGED;
        String providerEnabledKey = LocationManager.KEY_PROVIDER_ENABLED;

        if (intent.hasExtra(providerEnabledKey)) {
            if (intent.getBooleanExtra(providerEnabledKey, true)) {
                sendIntent(context, -1.0, -1.0, CommonModuleApp.KEY_INTENT_ACTION_GPS_TO_MAIN_APP, CommonModuleApp.KEY_SEARCH_SEARCH, -1);
            }
        }

        if (intent.hasExtra(locationKey)) {
            Location currentLocation = (Location)intent.getExtras().get(locationKey);

            double latitude = currentLocation.getLatitude();
            double longitude = currentLocation.getLongitude();
            float speed = currentLocation.getSpeed();
            double altitude = currentLocation.getAltitude();

            Log.d(CommonModuleApp.TAG_LOG_CAT, "GpsTrackerBroadcastReceiver onReceive");
            Log.d(CommonModuleApp.TAG_LOG_CAT, "Coordinates: Lat: "+ latitude +" Long: "+ longitude);

            if (currentLocation.getAccuracy() <= 15){
                sendIntent(context, latitude, longitude, CommonModuleApp.KEY_INTENT_ACTION_GPS_TO_MAIN_APP, CommonModuleApp.KEY_SEARCH_FIND, altitude);
            }

            sendIntentToService(context, latitude, longitude, speed, altitude, CommonModuleApp.KEY_INTENT_ACTION_GPS);
        }
    }

    private void sendIntentToService(Context context, double latitude, double longitude, float speed, double altitude, String keyIntentActionGps) {
        Intent intentTemp = new Intent();
        intentTemp.setAction(keyIntentActionGps);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LAT, latitude);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LONG, longitude);

        intentTemp.putExtra(CommonModuleApp.KEY_GPS_SPEED, speed);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_ALTITUDE, altitude);

        context.sendBroadcast(intentTemp);
    }

    private void sendIntent(Context context, double latitude, double longitude, String action, String searchCode, double altitude) {
        Intent intentTemp = new Intent();
        intentTemp.setAction(action);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LAT, latitude);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LONG, longitude);
        intentTemp.putExtra(CommonModuleApp.KEY_SEARCH_SATELITE, searchCode);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_ALTITUDE, altitude);

        context.sendBroadcast(intentTemp);
    }
}
